package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.fliter.PhotoProcessing;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.PictureFilterAdapter;
import flc.ast.bean.MyPictureFilterBean;
import flc.ast.databinding.ActivityPicFilterBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class PicFilterActivity extends BaseAc<ActivityPicFilterBinding> {

    public static String imgPath = "";
    private PictureFilterAdapter filterAdapter;
    private int oldPosition = 0;
    private Bitmap mImgBitmap;//原图
    private Bitmap mFilterBitmap;//添加滤镜后的图
    private Dialog myTipDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_filter;
    }

    @Override
    public void onBackPressed() {
        myTipDialog.show();
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicFilterBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivFilterSave.setOnClickListener(this);
        mDataBinding.rvPicFilterRatioList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        filterAdapter = new PictureFilterAdapter();
        mDataBinding.rvPicFilterRatioList.setAdapter(filterAdapter);
        filterAdapter.setOnItemClickListener(this);
        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivFilterSave:
                if (mFilterBitmap != null) {
                    saveImg(mFilterBitmap);
                } else {
                    saveImg(mImgBitmap);
                }
                break;
            //提示弹框
            case R.id.ivDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.ivDialogTipRight://确定
                myTipDialog.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        filterAdapter.getItem(oldPosition).setSelect(false);
        filterAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        filterAdapter.getItem(position).setSelect(true);
        filterAdapter.notifyItemChanged(position);
        setFilter(position);
    }

    @Override
    protected void initData() {
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        mImgBitmap = ImageUtils.getBitmap(imgPath);
        Glide.with(this).load(imgPath).into(mDataBinding.ivPicFilterImg);
        mDataBinding.ivPicFilterImg.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.ivPicFilterImg.setScaleEnabled(false);// 禁用缩放
        //滤镜数据
        String[] mFilters = getResources().getStringArray(R.array.filters);
        List<MyPictureFilterBean> listFilter = new ArrayList<>();
        listFilter.add(new MyPictureFilterBean(mFilters[0], "#00000000", true));
        listFilter.add(new MyPictureFilterBean(mFilters[1], "#323333", false));
        listFilter.add(new MyPictureFilterBean(mFilters[2], "#C376F3", false));
        listFilter.add(new MyPictureFilterBean(mFilters[3], "#6F7171", false));
        listFilter.add(new MyPictureFilterBean(mFilters[4], "#505151", false));
        listFilter.add(new MyPictureFilterBean(mFilters[5], "#999999", false));
        listFilter.add(new MyPictureFilterBean(mFilters[6], "#513232", false));
        listFilter.add(new MyPictureFilterBean(mFilters[7], "#787A7A", false));
        listFilter.add(new MyPictureFilterBean(mFilters[8], "#5000FF00", false));
        listFilter.add(new MyPictureFilterBean(mFilters[9], "#500000FF", false));
        listFilter.add(new MyPictureFilterBean(mFilters[10], "#50FF0000", false));
        listFilter.add(new MyPictureFilterBean(mFilters[11], "#50FFFF00", false));
        filterAdapter.setList(listFilter);
    }

    //设置滤镜
    private void setFilter(final int f) {
        showDialog(getString(R.string.set_filter_ing));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Bitmap retBitmap = Bitmap.createBitmap(mImgBitmap.copy(
                        Bitmap.Config.ARGB_8888, true));
                PhotoProcessing.filterPhoto(retBitmap, f);
                emitter.onNext(retBitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (bitmap == null) return;
                mFilterBitmap = bitmap;
                mDataBinding.ivPicFilterImg.setImageBitmap(mFilterBitmap);
            }
        });
    }

    //保存图片
    private void saveImg(Bitmap bitmap) {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogTipCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogTipRight);
        TextView textView=inflate.findViewById(R.id.tvDialogTipText);
        textView.setText(getString(R.string.image_not_save));
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}